********************************************************************************
* ELECTRICITY SPILOVERS PROJECT
*  COST EFFECTIVENESS 
********************************************************************************
set more off
frame copy data_main cost_effectiveness
frame change cost_effectiveness
keep if post==1

***************************************************
*Electricity Wholesale, Retail, and Expenditure Change
* Notes: - Calculates change in wholesale electricity aquisitions, retail
*           electricity expenditures, and the difference between the two. 
*        - BWP charges $0.1153/kWh for first 300 kWh, and $0.1672/kWh for the
*           remaining kWh. To calculate electricity expenditures, need to 
*           calculate aggregate monthly consumption. 
*        - Wholesale prices ($/MWH) for each hour and date come from CAISO 
*           OASIS SCE DLAP. 
*Total electricity use by HHID & month
sort HHID date hour 
bys HHID ym: gen euse_month = sum(euse)

*Merging in wholesale electricity cost data
merge m:1 date hour using $clean\wholesale_elec_20160906.dta
	drop if _merge==2 //Price data not in usage data
	drop _merge
	
*Retail cost
gen ret_cost_elec=.
	replace ret_cost_elec=0.1153 if euse_month<=300
	replace ret_cost_elec=0.1672 if euse_month>300

*Retail marginal cost
egen ret_mc_elec = max(ret_cost_elec), by(HHID ym) 

*Retail Electricity Expenditures
gen ret_expen_elec=euse*ret_mc_elec

*Wholesale Electricity Expenditures
gen whole_expen_elec=euse*elec_mc/1000

*Net Electricity Expenditure (Retail-Wholesale)
gen net_expen_elec= ret_expen_elec-whole_expen_elec


****************************************
*Damages from Electricity Production 
* Notes: - Calculates change CO2 and local air pollution damages using 
*           CA specific damages from Holland, Mansur, Muller, and Yates (2016).

*Merging CO2 damages
merge m:1 hour using $data_main\Holland_MDelectric_carbon_2011.dta
	drop _merge
drop co2_md_kwh_ercot co2_md_kwh_wecc co2_md_kwh_frcc co2_md_kwh_mro ///
	 co2_md_kwh_npcc co2_md_kwh_rfc co2_md_kwh_serc co2_md_kwh_spp

*Merging Local Electric Damages
merge m:1 hour using $data_main\Holland_MDelectric_local_2011_Roman.dta
	drop _merge
rename ca local_md_kwh_ca
drop ercot wecc frcc mro npcc rfc serc spp
	
*Wholesale Electricity Expenditures
gen co2_dam_elec=euse*co2_md_kwh_ca

gen local_dam_elec=euse*local_md_kwh_ca


***************************************************
*Water Wholesale, Retail, and Expenditure Change
* Notes: - Calculates change in wholesale water aquisitions, retail
*           water expenditures, and the difference between the two. 
*        - BWP charges $1.291/HCF for forst 15 HCF, $1.59/HCF for next 15 HCF,
*           and $2.001/HCF for anything over 30 HCF. All fees are augmented by
*           a water cost adjustment charge (WCAC) of $1.735/HCF. 
*        - Note: 1 HCF is 748.05 gals. 
*        - Taking into account both the block charge and WCAC, retail rates are 
*           $4.045/1000 gals for first 11,220.78 gals, 
*           $4.445/1000 gals for 11,220.78gals to 22,441.56 gals, and
*           $4.994/1000 gals for anything over 22,441.56 gals.
*        - Wholesale prices from BWP audit assumes $2.77/1000 gals from LADWP.
*Total water use by HHID & month
sort HHID date hour 
bys HHID ym: gen wuse_month = sum(wuse)

*Retail Water Cost
gen ret_cost_wtr=.
	replace ret_cost_wtr=4.045/1000 if wuse_month<=1120.78
	replace ret_cost_wtr=4.445/1000 if wuse_month>1120.78 & wuse_month<=22441.56
	replace ret_cost_wtr=4.994/1000 if wuse_month>22441.56

*Retail Water Marginal Cost
egen ret_mc_wtr = max(ret_cost_wtr), by(HHID ym) 
	
*Retail Water Expenditures
gen ret_expen_wtr=wuse*ret_mc_wtr

*Wholesale Water Marginal Cost
gen whole_expen_wtr=wuse*2.77/1000


***************************************
***************************************
*TABLE E.1 & E.2 - ELECTRICITY EXPENDITURE AND DAMAGE REGRESSIONS
* Notes: - May to August has 123 days
***************************************
***************************************
eststo clear
set more off
	
*Column 1: Retail Expenditures - Full Diff + weather + date + HOD FE + pre-treatment use
	eststo: reghdfe ret_expen_elec ws ///
	        elec_use_summer elec_use_annual elec_use_winter ///
			temp_65 temp_70 temp_75 temp_80 temp_85 precip_1, ///
			absorb(hour date) vce(cluster HHID)
	sum ret_expen_elec if e(sample) & ws==0
		scalar euse1 = r(mean) 
		estadd scalar m_cont = scalar(euse1)		
	lincom _b[ws]*24*365*(-1)
		scalar sav1 = r(estimate) 
		estadd scalar esave = scalar(sav1)	
		estadd local weath "Yes"
		estadd local hod "Yes"	
		estadd local date "Yes"
	est store A

*Column 2: Retail Expenditures (Summer only)- Full Diff + weather + date + HOD FE + pre-treatment use
	eststo: reghdfe ret_expen_elec ws ///
	        elec_use_summer elec_use_annual elec_use_winter ///
			temp_65 temp_70 temp_75 temp_80 temp_85 precip_1 if ym<=667, ///
			absorb(hour date) vce(cluster HHID)
	sum ret_expen_elec if e(sample) & ws==0
		scalar euse2 = r(mean) 
		estadd scalar m_cont = scalar(euse2)		
	lincom _b[ws]*24*123*(-1)
		scalar sav2 = r(estimate) 
		estadd scalar esave = scalar(sav2)	
		estadd local weath "Yes"
		estadd local hod "Yes"	
		estadd local date "Yes"
	est store B

*Column 3: Wholesale Expenditures - Full Diff + weather + date + HOD FE + pre-treatment use
	eststo: reghdfe whole_expen_elec ws ///
			elec_use_summer elec_use_annual elec_use_winter ///
			temp_65 temp_70 temp_75 temp_80 temp_85 precip_1, ///
			absorb(hour date) vce(cluster HHID)
	sum whole_expen_elec if e(sample) & ws==0
		scalar euse3 = r(mean) 
		estadd scalar m_cont = scalar(euse3)		
	lincom _b[ws]*24*365*(-1)
		scalar sav3 = r(estimate) 
		estadd scalar esave = scalar(sav3)	
		estadd local weath "Yes"
		estadd local hod "Yes"	
		estadd local date "Yes"
	est store C	

*Column 4: Wholesale Expenditures - Full Diff + weather + date + HOD FE + pre-treatment use
	eststo: reghdfe whole_expen_elec ws ///
			elec_use_summer elec_use_annual elec_use_winter ///
			temp_65 temp_70 temp_75 temp_80 temp_85 precip_1 if ym<=667, ///
			absorb(hour date) vce(cluster HHID)
	sum whole_expen_elec if e(sample) & ws==0
		scalar euse4 = r(mean) 
		estadd scalar m_cont = scalar(euse4)		
	lincom _b[ws]*24*123*(-1)
		scalar sav4 = r(estimate) 
		estadd scalar esave = scalar(sav4)	
		estadd local weath "Yes"
		estadd local hod "Yes"	
		estadd local date "Yes"
	est store D		
		
*Column 5: C02 Damages - Full Diff + weather + date + HOD FE + pre-treatment use
	eststo: reghdfe co2_dam_elec ws ///
	        elec_use_summer elec_use_annual elec_use_winter ///
			temp_65 temp_70 temp_75 temp_80 temp_85 precip_1, ///
			absorb(hour date) vce(cluster HHID)
	sum co2_dam_elec if e(sample) & ws==0
		scalar euse5 = r(mean) 
		estadd scalar m_cont = scalar(euse5)		
	lincom _b[ws]*24*365*(-1)
		scalar sav5 = r(estimate) 
		estadd scalar esave = scalar(sav5)
		estadd local weath "Yes"
		estadd local hod "Yes"	
		estadd local date "Yes"
	est store E	
	
*Column 6: C02 Damages - Full Diff + weather + date + HOD FE + pre-treatment use
	eststo: reghdfe co2_dam_elec ws ///
	        elec_use_summer elec_use_annual elec_use_winter ///
			temp_65 temp_70 temp_75 temp_80 temp_85 precip_1 if ym<=667, ///
			absorb(hour date) vce(cluster HHID)
	sum co2_dam_elec if e(sample) & ws==0
		scalar euse6 = r(mean) 
		estadd scalar m_cont = scalar(euse6)		
	lincom _b[ws]*24*123*(-1)
		scalar sav6 = r(estimate) 
		estadd scalar esave = scalar(sav6)
		estadd local weath "Yes"
		estadd local hod "Yes"	
		estadd local date "Yes"
	est store F		
		
*Column 7: Local Damages - Full Diff + weather + date + HOD FE + pre-treatment use
	eststo: reghdfe local_dam_elec ws ///
	        elec_use_summer elec_use_annual elec_use_winter ///
			temp_65 temp_70 temp_75 temp_80 temp_85 precip_1, ///
			absorb(hour date) vce(cluster HHID)
	sum local_dam_elec if e(sample) & ws==0
		scalar euse7 = r(mean) 
		estadd scalar m_cont = scalar(euse7)		
	lincom _b[ws]*24*365*(-1)
		scalar sav7 = r(estimate) 
		estadd scalar esave = scalar(sav7)
		estadd local weath "Yes"
		estadd local hod "Yes"	
		estadd local date "Yes"
	est store G
	
*Column 8: Local Damages - Full Diff + weather + date + HOD FE + pre-treatment use
	eststo: reghdfe local_dam_elec ws ///
	        elec_use_summer elec_use_annual elec_use_winter ///
			temp_65 temp_70 temp_75 temp_80 temp_85 precip_1 if ym<=667, ///
			absorb(hour date) vce(cluster HHID)
	sum local_dam_elec if e(sample) & ws==0
		scalar euse8 = r(mean) 
		estadd scalar m_cont = scalar(euse8)		
	lincom _b[ws]*24*123*(-1)
		scalar sav8 = r(estimate) 
		estadd scalar esave = scalar(sav8)
		estadd local weath "Yes"
		estadd local hod "Yes"	
		estadd local date "Yes"
	est store H	
	
esttab A B C D E F G H using $tables\eregs_CE_$outputdate.tex, replace label ///
    booktabs b(a2) nonumber ///
	drop(temp_65 temp_70 temp_75 temp_80 temp_85 precip_1) ///
    starlevels(* 0.10 ** 0.05 *** 0.01) ///
	title(Effects of HWRs on Electricity Expenditures and Externalities ///
	     \label{eregs_ce1})  ///
	cells(b(fmt(5) star) se(fmt(5) par)) ///
	note(Notes: The table reports intent-to-treat results from an OLS ///
		 regression of either (i) retail electricity expeditures, ///
		 (ii) wholesale electricity expenditures, (iii) CO$_2$ damages from ///
		 electric generation for CA, or ///
		 (iv) local damages from electric generation for CA on assignment ///
		 to the treatment.  Standard errors are ///
		 clustered at the household. *, **, *** denote significance at the ///
		 10\%, 5\%, and 1\%level.) ///
	scalars("m_cont Mean Control Group" "esave Year Impact" ///
	        "weath Weather Controls" /// 
		    "hod Hour of Day FE" "date Calendar Date FE") ///
	 mlabels((1) (2) (3) (4) (5) (6) (7) (8)) collabels(none) 
	
	

***************************************
***************************************
*Electricity Regressions - Month-by-Hour
***************************************
***************************************
*Creating days in month
gen month_days=.
	replace month_days=30 in 1  //June 2015
	replace month_days=31 in 2  //July 2015
	replace month_days=31 in 3  //August 2015
	replace month_days=30 in 4  //September 2015
	replace month_days=31 in 5  //October 2015
	replace month_days=30 in 6  //November 2015
	replace month_days=31 in 7  //December 2015
	replace month_days=31 in 8  //January 2016
	replace month_days=29 in 9 //February 2016
	replace month_days=31 in 10 //March 2016
	replace month_days=30 in 11 //April 2016
	replace month_days=31 in 12 //May 2016
	
***************************
*MONTH BY HOUR-OF-DAY ELECTRICITY SAVINGS
***************************	
*Electricity estimates by month & hour of day
forvalues j=0(1)23{
		gen elec_reta_save_month_h`j'=.
		gen elec_retb_save_month_h`j'=.

		gen elec_wholea_save_month_h`j'=.
		gen elec_wholeb_save_month_h`j'=.

		gen elec_co2a_save_month_h`j'=.
		gen elec_co2b_save_month_h`j'=.

		gen elec_locala_save_month_h`j'=.
		gen elec_localb_save_month_h`j'=.
}
*
set more off				
forvalues h=0(1)23{
	forvalues i=1(1)12{
		local j=`i'+664
			
	*Days in Month
	scalar month_day`j'=month_days[`i'] //Days in month
			
	****
	*Retail Expenditures - No Pre-Treatment Controls
	reghdfe ret_expen_elec ws ///
			if ym==`j' & hour==`h', absorb(date)  
				
	*Value of Retail Reductions
	lincom _b[ws]*scalar(month_day`j')
		replace elec_reta_save_month_h`h'=r(estimate) in `i'
	
	****
	*Retail Expenditures - Pre-Treatment Controls
	reghdfe ret_expen_elec ws elec_use_summer elec_use_annual elec_use_winter ///
			if ym==`j' & hour==`h', absorb(date)  
				
	*Value of Retail Reductions
	lincom _b[ws]*scalar(month_day`j')
		replace elec_retb_save_month_h`h'=r(estimate) in `i'
	
	****
	*Wholesale Expenditures - No Pre-Treatment Controls
	reghdfe whole_expen_elec ws ///
			if ym==`j' & hour==`h', absorb(date)  
				
	*Value of Retail Reductions
	lincom _b[ws]*scalar(month_day`j')
		replace elec_wholea_save_month_h`h'=r(estimate) in `i'
	
	****
	*Wholesale Expenditures - Pre-Treatment Controls
	reghdfe whole_expen_elec ws elec_use_summer elec_use_annual elec_use_winter ///
			if ym==`j' & hour==`h', absorb(date)  
				
	*Value of Retail Reductions
	lincom _b[ws]*scalar(month_day`j')
		replace elec_wholeb_save_month_h`h'=r(estimate) in `i'
		
	****
	*C02 Damages - No Pre-Treatment Controls
	reghdfe co2_dam_elec ws ///
			if ym==`j' & hour==`h', absorb(date)  
				
	*Value of Retail Reductions
	lincom _b[ws]*scalar(month_day`j')
		replace elec_co2a_save_month_h`h'=r(estimate) in `i'
	
	****
	*C02 Damages - Pre-Treatment Controls
	reghdfe co2_dam_elec ws elec_use_summer elec_use_annual elec_use_winter ///
			if ym==`j' & hour==`h', absorb(date)  
				
	*Value of Retail Reductions
	lincom _b[ws]*scalar(month_day`j')
		replace elec_co2b_save_month_h`h'=r(estimate) in `i'
		
		disp `h'
		disp `j'
		
	****
	*Local Damages - No Pre-Treatment Controls
	reghdfe local_dam_elec ws ///
			if ym==`j' & hour==`h', absorb(date)  
				
	*Value of Retail Reductions
	lincom _b[ws]*scalar(month_day`j')
		replace elec_locala_save_month_h`h'=r(estimate) in `i'
	
	****
	*Local Damages - Pre-Treatment Controls
	reghdfe local_dam_elec ws elec_use_summer elec_use_annual elec_use_winter ///
			if ym==`j' & hour==`h', absorb(date)  
				
	*Value of Retail Reductions
	lincom _b[ws]*scalar(month_day`j')
		replace elec_localb_save_month_h`h'=r(estimate) in `i'
		
		disp `h'
		disp `j'		
}
*
}
*
keep elec_reta_save_month_h0-month_days
drop if missing( elec_reta_save_month_h0)


***************************************
***************************************
*TABLE E.3 - WATER EXPENDITURE REGRESSIONS
***************************************
***************************************
eststo clear
set more off

*Column 1: Retail Expenditures - Full Diff + weather + date + HOD FE
	eststo: reghdfe ret_expen_wtr ws ///
			temp_65 temp_70 temp_75 temp_80 temp_85 precip_1, ///
			absorb(hour date) vce(cluster HHID)
	sum ret_expen_wtr if e(sample) & ws==0
		scalar wuse1 = r(mean) 
		estadd scalar m_cont = scalar(wuse1)		
	lincom _b[ws]*24*365
		scalar sav1 = r(estimate) 
		estadd scalar esave = scalar(sav1)		
		estadd local weath "Yes"
		estadd local hod "Yes"	
		estadd local date "Yes"
	est store A
	
*Column 2: Retail Expenditures - Full Diff + weather + date + HOD FE + pre-treatment use
	eststo: reghdfe ret_expen_wtr ws ///
	        elec_use_summer elec_use_annual elec_use_winter ///
			temp_65 temp_70 temp_75 temp_80 temp_85 precip_1, ///
			absorb(hour date) vce(cluster HHID)
	sum ret_expen_wtr if e(sample) & ws==0
		scalar wuse2 = r(mean) 
		estadd scalar m_cont = scalar(wuse2)		
	lincom _b[ws]*24*365
		scalar sav2 = r(estimate) 
		estadd scalar esave = scalar(sav2)	
		estadd local weath "Yes"
		estadd local hod "Yes"	
		estadd local date "Yes"
	est store B
			
*Column 3: Wholesale Expenditures - Full Diff + weather + date + HOD FE
	eststo: reghdfe whole_expen_wtr ws ///
	        temp_65 temp_70 temp_75 temp_80 temp_85 precip_1, ///
			absorb(hour date) vce(cluster HHID)
	sum whole_expen_wtr if e(sample) & ws==0
		scalar wuse3 = r(mean) 
		estadd scalar m_cont = scalar(wuse3)		
	lincom _b[ws]*24*365
		scalar sav3 = r(estimate) 
		estadd scalar esave = scalar(sav3)	
		estadd local weath "Yes"
		estadd local hod "Yes"	
		estadd local date "Yes"
	est store C
	
*Column 4: Wholesale Expenditures - Full Diff + weather + date + HOD FE + pre-treatment use
	eststo: reghdfe whole_expen_wtr ws ///
			elec_use_summer elec_use_annual elec_use_winter ///
			temp_65 temp_70 temp_75 temp_80 temp_85 precip_1, ///
			absorb(hour date) vce(cluster HHID)
	sum whole_expen_wtr if e(sample) & ws==0
		scalar wuse4 = r(mean) 
		estadd scalar m_cont = scalar(wuse4)		
	lincom _b[ws]*24*365
		scalar sav4 = r(estimate) 
		estadd scalar esave = scalar(sav4)	
		estadd local weath "Yes"
		estadd local hod "Yes"	
		estadd local date "Yes"
	est store D	
	
esttab A B C D using $tables\wregs_CE_$outputdate.tex, replace label ///
    booktabs b(a2) nonumber ///
	drop(temp_65 temp_70 temp_75 temp_80 temp_85 precip_1) ///
    starlevels(* 0.10 ** 0.05 *** 0.01) ///
	title(Effects of HWRs on Electricity Expenditures and Externalities ///
	     \label{eregs_ce1})  ///
	cells(b(fmt(3) star) se(fmt(3) par)) ///
	note(Notes: The table reports intent-to-treat results from an OLS ///
		 regression of either (i) retail electricity expeditures, ///
		 (ii) wholesale electricity expenditures, (iii) CO$_2$ damages from ///
		 electric generation for CA, or ///
		 (iv) local damages from electric generation for CA on assignment ///
		 to the treatment.  Standard errors are ///
		 clustered at the household. *, **, *** denote significance at the ///
		 10\%, 5\%, and 1\%level.) ///
	scalars("m_cont Mean Control Group" "esave Year Impact" ///
	        "weath Weather Controls" /// 
		    "hod Hour of Day FE" "date Calendar Date FE") ///
	 mlabels((1) (2) (3) (4)) collabels(none) 
	
	
****************************************
*Marginal CO2 Emissions from Electricity in WECC
*Notes: - Source: Graff Zivin, Kotchen, and Mansur (2014)	
*       - Estimates in average pounts of marginal CO2 emission reductions per 
*          kwh.  
*       - To convert to lbs of CO2 saved, multiply by treatment effect of 
*          kwh/hour. 
*       - To convert to metric tons, divide by 2204.62. 
*       - Assume $36/metric ton for CO2 reductions (consistent with EPA SCC)
gen co2_savings=.
	label var co2_savings "Marginal CO2 Electricity Rates in WECC (lbs/kwh)"
	replace co2_savings=0.84 if hour==0
	replace co2_savings=0.83 if hour==1
	replace co2_savings=0.84 if hour==2
	replace co2_savings=0.84 if hour==3
	replace co2_savings=0.80 if hour==4
	replace co2_savings=0.77 if hour==5
	replace co2_savings=0.71 if hour==6
	replace co2_savings=0.66 if hour==7
	replace co2_savings=0.68 if hour==8
	replace co2_savings=0.77 if hour==9
	replace co2_savings=0.85 if hour==10
	replace co2_savings=0.88 if hour==11
	replace co2_savings=0.88 if hour==12
	replace co2_savings=0.86 if hour==13
	replace co2_savings=0.83 if hour==14
	replace co2_savings=0.82 if hour==15
	replace co2_savings=0.80 if hour==16
	replace co2_savings=0.79 if hour==17
	replace co2_savings=0.79 if hour==18
	replace co2_savings=0.80 if hour==19
	replace co2_savings=0.81 if hour==20
	replace co2_savings=0.80 if hour==21
	replace co2_savings=0.81 if hour==22
	replace co2_savings=0.82 if hour==23
	

***************************
*MONTHLY ELECTRICITY SAVINGS
***************************	
*Electricity estimates by month
gen m_save=.
gen elec_save_month=.
gen elec_save_value_month=.
gen elec_save_co2_month=.

*Creating days in month
gen month_days=.
	replace month_days=17 in 1  //May 15, 2015 - May 31, 2015
	replace month_days=30 in 2  //June 2015
	replace month_days=31 in 3  //July 2015
	replace month_days=31 in 4  //August 2015
	replace month_days=30 in 5  //September 2015
	replace month_days=31 in 6  //October 2015
	replace month_days=30 in 7  //November 2015
	replace month_days=31 in 8  //December 2015
	replace month_days=31 in 9  //January 2016
	replace month_days=29 in 10 //February 2016
	replace month_days=31 in 11 //March 2016
	replace month_days=30 in 12 //April 2016
	replace month_days=31 in 13 //May 2016

forvalues i=1(1)13{
	local j=`i'+663
	
	replace m_save=`j' in `i'
	
	*Monthly ITT Effect
	reghdfe euse temp_sp1 temp_sp2 precip ws if ym==`j', ///
		absorb(hour date) vce(cluster HHID)
	
	*Electricity Savings
	lincom -(1)*_b[ws]
	replace elec_save_month=r(estimate) in `i'
	
	
	*Value of Electricity Savings
	sum elec_mc if e(sample) //Avg. month electricity costs
 	scalar elec_cost_m`j' = r(mean) 
	scalar month_day`j'=month_days[`i'] //Days in month
	
	lincom -(1)*_b[ws]*24*scalar(month_day`j')*scalar(elec_cost_m`j')/1000
	replace elec_save_value_month=r(estimate) in `i'	
	
	*Value of CO2 Savings
	sum co2_savings if e(sample) 
	scalar co2_m`j' = r(mean)	
	lincom -(1)*_b[ws]*24*scalar(month_day`j')*scalar(co2_m`j')/2204.62*36
	replace elec_save_co2_month=r(estimate) in `i'	
}
*	
	
***************************
*MONTH BY HOUR-OF-DAY ELECTRICITY SAVINGS
***************************	
*Electricity estimates by month & hour of day
forvalues j=0(1)23{
		gen elec_save_month_h`j'=.
		gen elec_save_value_month_h`j'=.
		gen elec_save_co2_month_h`j'=.
}
*
set more off				
forvalues h=0(1)23{
	forvalues i=1(1)13{
		local j=`i'+663
				
		*Month-Hour ITT Effect
		reghdfe euse ws if ym==`j' & hour==`h', ///
			absorb(date) vce(cluster HHID)

		*Electricity Savings
		lincom -(1)*_b[ws]
		replace elec_save_month_h`h'=r(estimate) in `i'

		*Value of Electricity Savings
		sum elec_mc if e(sample)
		scalar elec_cost_m`j'_h`h' = r(mean)
		lincom -(1)*_b[ws]*scalar(month_day`j')*scalar(elec_cost_m`j'_h`h')/1000
		replace elec_save_value_month_h`h'=r(estimate) in `i'

		*Value of CO2 Savings	
		sum co2_savings if e(sample) 
		scalar co2_m`j'_h`h' = r(mean)	
		lincom -(1)*_b[ws]*scalar(month_day`j')*scalar(co2_m`j'_h`h')/2204.62*36
		replace elec_save_co2_month_h`h'=r(estimate) in `i'

		disp `h'
		disp `j'
}
*
}
*
*Formating hours and graphing
format m_save %tm	
	  
forvalues h=0(1)23{
	replace elec_save_month_h`h'=-elec_save_month_h`h'
	label var elec_save_month_h`h' "`h'"
	label var elec_save_value_month_h`h' "`h'"
	label var elec_save_co2_month_h`h' "`h'"

	}
	*	
*Saving estimated value of savings
keep co2_savings wat_save wat_save_value elec_save elec_save_value ///
	 elec_save_co2 m_save elec_save_month elec_save_value_month ///
	 elec_save_co2_month month_days elec_save_month_h* ///
	 elec_save_value_month_h* elec_save_co2_month_h*  
drop if missing(elec_save_co2_month_h0)

sa $clean\value_te_$outputdate.dta, replace

frame change data_main
frame drop cost_effectiveness 